package com.zjm.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.zjm.entity.*;
import com.zjm.service.TeacherService;
import com.zjm.service.impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.io.File;


import com.zjm.entity.DemoData;
import com.zjm.entity.UserInfoInsertDaoLisenter;


@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService service;

    @RequestMapping("/registerTeacher.do")
    public ModelAndView registerTeacher(Teacher teacher,String confirmPassword){
        ModelAndView mv = new ModelAndView();
        Map<String,Object> map = new HashMap<>();
        System.out.println(teacher+confirmPassword);
        map = service.register(teacher,confirmPassword);
        if(map.get("msg")==null){
            mv.setViewName("ULogin");
            return mv;
        }
        String msg = teacher.getName()+map.get("msg");
        System.out.println(msg);
        mv.addObject("tips",msg);
        mv.setViewName("URegister");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/addTeacherName.do",produces = "text/plain;charset=utf-8")
    public String addTeacherName(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        System.out.println("teacher"+name);
        return name;
    }


    @RequestMapping("/teacherPublish.do")
    public ModelAndView teacherPublish(HttpServletRequest request,String clas_name,Integer amount,String clas_time){
        ModelAndView mv = new ModelAndView();
        String teacher_name = (String) request.getSession().getAttribute("name");
        Integer teacher_id = (Integer) request.getSession().getAttribute("id");
        Clas clas = new Clas();
        clas.setClas_time(clas_time);
        clas.setClas_name(clas_name);
        clas.setTeacher_id(teacher_id);
        clas.setTeacher_name(teacher_name);
        clas.setAmount(amount);
        System.out.println(clas);
        service.publishClas(clas);
        mv.setViewName("TPublishedClas");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/showTeacherClas.do")
    public List<Clas> showTeacherClas(HttpServletRequest request){
        Integer student_id = (Integer) request.getSession().getAttribute("id");
        List<Clas> clasList = service.showMyClas(student_id);
        System.out.println(clasList);
        return clasList;
    }

    @ResponseBody
    @RequestMapping("/showAllChangeMajor.do")
    public List<Change_major> showAllChangeMajor(HttpServletRequest request){
        System.out.println("==============转专业申请==============");
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        List<Change_major> changeMajorList = service.showChangeMajor(teacher);
        System.out.println(changeMajorList);
        return changeMajorList;
    }

    @RequestMapping("/publishClasScore.do")
    public ModelAndView publishClasScore(HttpServletRequest request,Integer clas_id){
        ModelAndView mv = new ModelAndView();
        System.out.println("发布成绩");
        request.getSession().setAttribute("clas_id",clas_id);
        List<Select_clas> selectClasList = service.showMyClasStudents(clas_id);
        System.out.println(selectClasList);
        mv.addObject(selectClasList);
        mv.setViewName("TPublishScore");
        return mv;
    }
    @RequestMapping("/updateClasScore.do")
    public ModelAndView updateClasScore(HttpServletRequest request,Integer student_id,Double score){
        ModelAndView mv = new ModelAndView();
        Integer clas_id = (Integer) request.getSession().getAttribute("clas_id");
        System.out.println("更新成绩");
        System.out.println(score+"                "+student_id);
        service.updateScore(clas_id,student_id,score);
        mv.setViewName("TPublishScore");
        return mv;
    }

    /*@RequestMapping(value="/{formName}")
    public String loginForm(@PathVariable String formName){
        return formName;
    }*/










    /*@RequestMapping(value = "/upload.do",method = RequestMethod.POST)
    public ModelAndView upload(@RequestParam("file")MultipartFile file,
                               HttpServletRequest request)throws IOException{
       ModelAndView mv = new ModelAndView();
        if (!file.isEmpty()) {
            String storePath= "D:\\Scores";//存放我们上传的文件路径
            String fileName = file.getOriginalFilename();
            File filepath = new File(storePath, fileName);
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();//如果目录不存在，创建目录
            }
            try {
                file.transferTo(new File(storePath+File.separator+fileName));//把文件写入目标文件地址
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("读Excel");
        String fileName = "D:\\Scores\\Score.xlsx";
        EasyExcel.read(fileName,DemoData.class, new UserInfoInsertDaoLisenter(service)).sheet().doRead();
        mv.setViewName("TPublishScore");
        return mv;
    }*/
    /*@RequestMapping(value = "/upload1.do",method = RequestMethod.GET)
    public void upload1(HttpServletRequest request){
        System.out.println("读Excel");
        String fileName = "D:\\Scores\\Score.xlsx";
        TeacherService service = new TeacherServiceImpl();
        EasyExcel.read(fileName,DemoData.class, new UserInfoInsertDaoLisenter(service)).sheet().doRead();
    }*/













    @ResponseBody
    @RequestMapping("/showAllSelectedClasStudent.do")
    public List<Select_clas> showAllSelectedClasStudent(HttpServletRequest request){
        System.out.println("==============获取课程信息==============");
        Integer clas_id = (Integer) request.getSession().getAttribute("clas_id");
        List<Select_clas> selectClasList = service.showMyClasStudents(clas_id);
        System.out.println("课程信息：课程号:"+clas_id);
        System.out.println(selectClasList);
        return selectClasList;
    }

    @RequestMapping("/passChangeMajor.do")
    public ModelAndView passChangeMajor(HttpServletRequest request,String pass,Integer student_id){
        ModelAndView mv = new ModelAndView();
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        System.out.println("通过："+pass+"      学号："+student_id);
        service.passChangeMajor(student_id,pass,teacher);
        mv.setViewName("TChangeMajor");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/showAllVacate.do")
    public List<Vacate> showAllVacate(HttpServletRequest request){
        System.out.println("==============请假申请==============");
        Integer teacher_id = (Integer) request.getSession().getAttribute("id");
        System.out.println(teacher_id);
        List<Vacate> vacateList = service.showVacates(teacher_id);
        System.out.println(vacateList);
        return vacateList;
    }

    @RequestMapping("/passVacate.do")
    public ModelAndView passVacate(HttpServletRequest request,String pass,Integer student_id,String reason){
        ModelAndView mv = new ModelAndView();
        System.out.println("通过："+pass+"      学号："+student_id+"       原因:"+reason);
        service.passVacate(student_id,pass,reason);
        mv.setViewName("TVacate");
        return mv;
    }

}
